package com.aaa.car.service.impl;

import com.aaa.car.entity.Brand;
import com.aaa.car.entity.CarType;
import com.aaa.car.mapper.BrandMapper;
import com.aaa.car.service.BrandService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Brand)表服务实现类
 *
 * @author YuXin
 * @since 2020-11-19 19:36:38
 */
@Service("brandService")
public class BrandServiceImpl implements BrandService {
    @Resource
    private BrandMapper brandMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Brand queryById(Long id) {
        return this.brandMapper.queryById(id);
    }

    /**
     * 查询
     *
     * @param
     * @param
     * @return 对象列表
     */
    @Override
    public Result queryAllByParam(Integer pageNo, Integer pageSize, Brand brand) {
        if (pageNo == 0 || pageSize == 0) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Brand> pageInfo = new PageInfo<>(brandMapper.queryAll(brand));
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), pageInfo);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param brand 实例对象
     * @return 对象列表
     */
    @Override
    public List<Brand> queryAll(Brand brand) {
        return this.brandMapper.queryAll(brand);
    }

    /**
     * 新增数据
     *
     * @param brand 实例对象
     * @return 实例对象
     */
    @Override
    public Brand insert(Brand brand) {
        this.brandMapper.insert(brand);
        return brand;
    }

    /**
     * 修改数据
     *
     * @param brand 实例对象
     * @return 实例对象
     */
    @Override
    public Brand update(Brand brand) {
        this.brandMapper.update(brand);
        return this.queryById(brand.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.brandMapper.deleteById(id) > 0;
    }
}